import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;


public class InetAddressDemo {

	// An IP address is either a 32-bit or 128-bit unsigned number used by IP
	// , a lower-level protocol on which protocols like UDP and TCP are built.
	//
	// unicast - An identifier for a single interface
	// unicast: The Unspecified Address -- Also called anylocal or wildcard address. Allows a server to accept a client connection on any interface, in case the server host has multiple interfaces
	// unicast: The Loopback Addresses -- This is the address assigned to the loopback interface.
	//
	// multicast - A packet sent to a multicast address is delivered to all interfaces identified by that address.
	//
	// domain name, host name, top level domain, subdomain, zones
	// domain name space consists of a tree of domain names
	// The Domain Name System is maintained by a distributed database system, which uses the client-server model. The nodes of this database are the name servers
	// DNS primarily uses User Datagram Protocol (UDP) on port number 53 to serve requests
	public static void main(String[] args) throws IOException {
		// no public constructor
		// get with DNS lookup
		InetAddress google = InetAddress.getByName("google.com");
		dmp(google);
		// get w/o DNS lookup, only validity check is performed
		InetAddress ip = InetAddress.getByName("224.0.0.1");
		dmp(ip);
		// loopback
		InetAddress any = InetAddress.getByName("0.0.0.0");
		dmp(any);
		// loopback can be done also by passing null
		InetAddress loopback = InetAddress.getByName("");
		dmp(loopback);
		// localhost
		InetAddress host = InetAddress.getLocalHost();
		dmp(host);
		//
		InetAddress[] all = InetAddress.getAllByName("google.com");
		// dmp all
		System.out.println(Arrays.toString(all));
	}
	
	public static void dmp(InetAddress address) throws IOException {
		System.out.printf("host name: %s%n",address.getHostName());
		// can call getAddress to get binary representation - byte[]
		System.out.printf("host IP address: %s%n",address.getHostAddress());
		// unicast
		System.out.printf("is any local address: %b%n",address.isAnyLocalAddress());
		System.out.printf("is loopback address: %b%n",address.isLoopbackAddress());
		System.out.printf("is multicast address: %b%n",address.isMulticastAddress());
		if (address.isMulticastAddress()) {
			
		}
		System.out.printf("is reacheble: %b%n",address.isReachable(1000));
		System.out.println("");
	}
}
